package com.tjcd.project.library_base.common.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;

import com.tjcd.project.library_base.R;
import com.tjcd.project.library_base.common.utils.MeasureUtil;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/8
 * @Description
 * @SpittingGrooves
 */
public class ThrowLayout extends FrameLayout {
    /**
     * 隐藏
     */
    public static final int STATUS_HIDE = 1001;
    /**
     * 异常
     */
    public static final int STATUS_ERROR = 1002;
    /**
     * 空数据
     */
    public static final int STATUS_NO_DATA = 1003;
    /**
     * 自定义
     */
    public static final int STATUS_CUSTOME = 1004;

    /**
     * 类型
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STATUS_HIDE, STATUS_ERROR, STATUS_NO_DATA, STATUS_CUSTOME})
    public @interface EmptyStatus {
    }

    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 状态  默认是隐藏
     */
    private int mStatus = STATUS_HIDE;
    /**
     * 页面异常展示的View
     */
    View throwStubView;
    /**
     * 整体
     */
    ViewGroup throwVg;
    /**
     * 异常图片
     */
    ImageView throwIv;
    /**
     * 异常语
     */
    TextView throwTv;
    /**
     * 异常信息
     */
    TextView throwMsgTv;
    /**
     * 重试按钮
     */
    Button throwBtn;
    /**
     * 自定义图片
     */
    private int cusRes;
    /**
     * 自定义message
     */
    private String cusMessage;
    /**
     * 自定义msg
     */
    private String cusMsg;
    /**
     * 自定义按钮文字
     */
    private String cusBtnMessage;
    /**
     * 自定义marginT
     */
    private int cusMarginT;
    /**
     * 按钮点击回调
     */
    private OnRetryListener mOnRetryListener;


    public ThrowLayout(@NonNull Context context) {
        super(context);
    }

    public ThrowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        View.inflate(mContext, R.layout.layout_common_error_stub, this);
        switchEmptyView();
    }

    /**
     * 隐藏视图
     */
    public void hide() {
        mStatus = STATUS_HIDE;
        switchEmptyView();
    }

    /**
     * 设置状态
     *
     * @param emptyStatus 异常状态
     */
    public void setEmptyStatus(@EmptyStatus int emptyStatus) {
        mStatus = emptyStatus;
        switchEmptyView();
    }

    /**
     * 获取状态
     *
     * @return 状态
     */
    public int getEmptyStatus() {
        return mStatus;
    }

    /**
     * 设置自定义内容
     *
     * @param message
     */
    public void setCustomMessage(String message) {
        setCustomMessage(message, R.mipmap.ic_launcher, "重试");
    }

    /**
     * 设置自定义内容
     *
     * @param message
     * @param res
     */
    public void setCustomMessage(String message, int res) {
        setCustomMessage(message, res, "重试");
    }

    /**
     * 设置自定义内容
     *
     * @param message
     * @param res
     * @param btnMsg
     */
    public void setCustomMessage(String message, int res, String btnMsg) {
        setCustomMessage(message, "", res, btnMsg, 0);
    }

    /**
     * 设置自定义内容
     *
     * @param message
     * @param res
     * @param btnMsg
     */
    public void setCustomMessage(String message, String msg, int res, String btnMsg, int mariginT) {
        cusRes = res <= 0 ? 0 : res;
        cusMessage = message;
        cusMsg = msg;
        cusBtnMessage = btnMsg;
        cusMarginT = mariginT;
    }

    /**
     * 设置图片隐藏显示
     *
     * @param visable
     */
    public void setImageVisable(int visable) {
        if (null != throwIv) {
            throwIv.setVisibility(visable);
        }
    }

    /**
     * 设置marginTop
     *
     * @param top
     */
    public void setMarginTop(int top) {
        if (null != throwVg) {
            MeasureUtil.setMargin(throwVg, 0, top, 0, 0);
        }
    }

    /**
     * 设置重试监听器
     *
     * @param retryListener 监听器
     */

    public void setRetryListener(OnRetryListener retryListener) {
        this.mOnRetryListener = retryListener;
    }

    /**
     * 点击重试监听器
     */
    public interface OnRetryListener {
        void onRetry();
    }

    /**
     * 切换异常视图
     */
    private void switchEmptyView() {
        switch (mStatus) {
            case STATUS_HIDE:                    // 隐藏
                setVisibility(GONE);
                break;
            case STATUS_ERROR:                   // 异常
                setVisibility(VISIBLE);
                inflateLayout(STATUS_ERROR);
                break;
            case STATUS_NO_DATA:                 // 空数据
                setVisibility(VISIBLE);
                inflateLayout(STATUS_NO_DATA);
                break;
            case STATUS_CUSTOME:                 // 自定义
                setVisibility(VISIBLE);
                inflateLayout(STATUS_CUSTOME);
                break;
            default:
                break;
        }
    }

    /**
     * 反射异常ViewStub里面的Layout
     *
     * @param status 异常状态
     */
    private void inflateLayout(@EmptyStatus int status) {
        if (throwStubView == null) {
            ViewStub temp = findViewById(R.id.common_error_vs);
            throwStubView = temp.inflate();
        }
        throwVg = findViewById(R.id.common_error_view_ll);
        throwIv = findViewById(R.id.common_error_view_iv);
        throwTv = findViewById(R.id.common_error_view_tv);
        throwMsgTv = findViewById(R.id.common_error_view_msg_tv);
        throwBtn = findViewById(R.id.common_error_view_btn);
        throwVg.setVisibility(VISIBLE);
        if (!TextUtils.isEmpty(cusMsg)) {
            throwMsgTv.setVisibility(VISIBLE);
        }
        switch (status) {
            case STATUS_ERROR:         // 异常情况
                MeasureUtil.setMargin(throwVg, 0, cusMarginT, 0, 0);
                throwIv.setImageResource(cusRes);
                throwTv.setText(cusMessage);
                throwBtn.setText(cusBtnMessage);
                throwMsgTv.setText(cusMsg);
                break;
            case STATUS_NO_DATA:       // 空数据
            case STATUS_CUSTOME:       // 自定义
                MeasureUtil.setMargin(throwVg, 0, cusMarginT, 0, 0);
                throwIv.setImageResource(cusRes);
                throwTv.setText(cusMessage);
                throwBtn.setText(cusBtnMessage);
                throwMsgTv.setText(cusMsg);
                break;
        }
        throwStubView.setVisibility(VISIBLE);
        if (null == mOnRetryListener) {
            throwBtn.setVisibility(GONE);
        } else {
            throwBtn.setVisibility(VISIBLE);
            throwBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnRetryListener != null) {
                        mOnRetryListener.onRetry();
                    }
                }
            });
        }
    }
}
